package com.lwl.service.impl.sas;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.dao.sas.TQuestionSasMapper;
import com.lwl.entity.MyPage;

import com.lwl.entity.sas.TQuestionSas;
import com.lwl.service.TQuestionSasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


@Service
public class TQuestionSasServicelmpl implements TQuestionSasService {
    @Autowired
    private TQuestionSasMapper tQuestionSasMapper;

    @Override
    public PageInfo queryAll (MyPage myPage, TQuestionSas tQuestionSas){
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<TQuestionSas> tQuestionSass = tQuestionSasMapper.selectAll();
        System.out.println("tQuestionSass%%%%%%%%%%%%%%%%%%%%%%% = " + tQuestionSass);
        PageInfo pageInfo = new PageInfo(tQuestionSass);
        return pageInfo;
    }
    @Override
    public boolean insertQuestion(TQuestionSas tQuestionSas){
        int insert = -1;
        if (tQuestionSas.getId() == null){
            insert = tQuestionSasMapper.insert(tQuestionSas);
        }else {
            insert = tQuestionSasMapper.updateByPrimaryKeySelective(tQuestionSas);
        }
        return insert>0?true:false;
    }
    @Override
    public boolean queryQuestion(String question) {
        Example example = new Example(TQuestionSas.class);
        example.createCriteria().andEqualTo("question",question);
        TQuestionSas tQuestionSas = tQuestionSasMapper.selectOneByExample(example);
        System.out.println("tQuestionSas = " + tQuestionSas);
        if (tQuestionSas==null){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteQuestion(Integer id) {
        int delete = tQuestionSasMapper.deleteByPrimaryKey(id);
        return delete>0?true:false;
    }

    @Override
    public boolean uploadExcel(MultipartFile file) {

        ExcelReaderBuilder read = null;
        try {
            read = EasyExcel.read(file.getInputStream());
            List<LinkedHashMap<String,Object>> objects = read.doReadAllSync();
            for (LinkedHashMap<String, Object> object : objects) {
                TQuestionSas tQuestionSas = new TQuestionSas();
                tQuestionSas.setQuestion((String) object.get(0));
                tQuestionSas.setOptiona((String) object.get(1));
                tQuestionSas.setOptionb((String) object.get(2));
                tQuestionSas.setOptionb((String) object.get(3));
                tQuestionSas.setOptionb((String) object.get(4));
//               personInfoDTO.setNickname((String) object.get(0));
                System.out.println("tQuestionSas = " + tQuestionSas);
                int i = tQuestionSasMapper.loginAddInfo(tQuestionSas);
                if(i<=0 ){
                    return false;
                }
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean dealFile(MultipartFile file) {
        // 如果试题已经存在就不让再导入了
        int i = tQuestionSasMapper.selectCount(null);
        if(i>0){
            return false;
        }else {
            InputStream is = null;
            try {
                is = file.getInputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //1.进行读取数据，TFpaSub是我的pojo类，
            //2.new FileListener(fileService)这个是监听器，主要用来读取数据的
            //3.特别注意的是fileService这个service，我上面有注入进去 @Autowired/@Resource，切记不要new会报错
            //EasyExcel.read(is, TFpaSub.class, new FileListener(fileService)).sheet("FPA性格测试").doRead();
            EasyExcel.read(is, TQuestionSas.class, new AnalysisEventListener<TQuestionSas>() {
                /**
                 * 单次缓存的数据量
                 */
                public static final int BATCH_COUNT = 100;
                /**
                 * 临时存储
                 */
                private List<TQuestionSas> cachedDataList = new ArrayList<>();

                @Override
                public void invoke(TQuestionSas tQuestionSas, AnalysisContext analysisContext) {
                    cachedDataList.add(tQuestionSas);
                    if (cachedDataList.size() >= BATCH_COUNT) {
                        saveData();
                        // 存储完成清理 list
                        cachedDataList = new ArrayList<>();
                    }
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {
                    saveData();
                }

                /**
                 * 加上存储数据库
                 */
                private void saveData() {
//            log.info("{}条数据，开始存储数据库！", cachedDataList.size());
                    save(cachedDataList);
                }
            }).sheet("Sas量表").doRead();
        }
        return false;
    }
    @Override
    public void save(List<TQuestionSas> list) {
        tQuestionSasMapper.insertFtaSubjects(list);
    }

    @Override
    public PageInfo getAll(MyPage myPage) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        return new PageInfo(tQuestionSasMapper.selectAll());
    }






}
